package net.zwet.clanworks.connectors.eve.api.model;

import net.zwet.clanworks.connectors.eve.api.utils.DateUtils;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class EveApi {

    private String currentTime;
    private String cachedUntil;
    private Integer version;

	private Error error;

	public EveApi() {
	}

    // * ************************************************************************************************************ *
    // * Getters & Setters                                                                                            *
    // * ************************************************************************************************************ *
    public Error getError() {
		return error;
	}

	public void setError(Error error) {
		this.error = error;
	}

    @XmlElement
	public String getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(String currentTime) {
		this.currentTime = currentTime;
	}

    @XmlElement
	public String getCachedUntil() {
		return cachedUntil;
	}

	public void setCachedUntil(String cachedUntil) {
		this.cachedUntil = cachedUntil;
	}

    @XmlAttribute
    public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public boolean isValid() throws ParseException {
		boolean isValid = true;
		
		// Version Test ?
		
		if ( isValid ) {
			Date dateNow = new Date();
			Date cachedUntil = DateUtils.convertToDate( this.getCachedUntil(), DateUtils.EvEDateFormatString );
	
			Calendar calCacheDate = Calendar.getInstance();
			Calendar calNow = Calendar.getInstance();
			
			calCacheDate.setTime(cachedUntil);
			calNow.setTime( DateUtils.eveTime( dateNow ) );
	
			long millisCacheDate = calCacheDate.getTimeInMillis();
			long millisFileDate = calNow.getTimeInMillis();
			
			isValid = ( millisFileDate <= millisCacheDate );
		}
		
		return isValid;
	}

	public boolean inValid() throws ParseException {
		return ! this.isValid();
	}
}

